package com.springmvc.demo.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.springmvc.demo.mapper.RoleMapper;
import com.springmvc.demo.mapper.UserMapper;
import com.springmvc.demo.model.Role;
import com.springmvc.demo.model.User;
import com.springmvc.demo.service.UserService;



@Controller
public class MainController {
	@Autowired
	UserMapper userMapper;
	
	@Autowired
	UserService userServiceImpl;

	
	@Autowired
	RoleMapper roleMapper;
	
	@RequestMapping("/")
	public String index(HttpSession session) {
		if(session.getAttribute("name")!=null)
			return "main";
		return "login";
	}
	
	
	@RequestMapping("/test")
	public String test(Map<String,Object> map) {
		map.put("message", "qqqqq");
		return "test";
	}
	
	@RequestMapping("/userManagement")
	public String userMangement() {
		
		return "usermanage";
	}
	

	/*
	 * 删除
	 */
	@RequestMapping("/deleteUser")
	@ResponseBody
	public String deleteUser(@RequestParam String userid,HttpSession session) {
		String rString= (String) session.getAttribute("roleid");
		if(!rString.equals("R1"))
			return "你没有权限";
		userMapper.deleteUser(userid);
		return "already delete ";
	}
	
	/*
	 * 添加用户
	 */
	@PostMapping("/addUser")
	@ResponseBody
	public String addUser(User u) {
		
		System.out.println(u.getRoleid());
		
		return userServiceImpl.addUser(u);
	}
	
	
	@RequestMapping("/listUser")
	@ResponseBody
	public JSONObject listUser() {
		JSONObject jsonObject=new JSONObject();
		List<?> list=userServiceImpl.getUserList();
		JSONArray jsonArray=(JSONArray) JSONArray.toJSON(list);
		jsonObject.put("data",jsonArray);
		return jsonObject;
	}
	
	
	@PostMapping("/addRole")
	@ResponseBody
	public String addRole(Role r) {
		System.out.println(r.getRolename());
		roleMapper.insertRole(r);
		
		return "already add";
	}
	
	
	
	/*
	 *家庭信息管理页面 
	 */
	@RequestMapping("/profileManagement")
	public String profileManagement() {
		return "homeProfileManagement";
	}
	
	
	/*
	 *慢性病管理页面 
	 */
	@RequestMapping("/chronicDiseaseManagement")
	public String chronicDiseaseManagement() {
		return "ChronicDisease";
	}
	/*
	 *慢性病报销页面 
	 */
	@RequestMapping("/chronicDiseaseRefund")
	public String chronicDiseaseRefund() {
		return "diseaseRefund";
	}
}
